package com.kehutong.crm.admin;
import org.coraframework.authz.HasPermission;
import org.coraframework.bean.valid.ValidResult;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;

import com.kehutong.crm.entity.TrackStatus;
/**
 * 跟踪状态
 * @author: liukunlong
 * @date: 2021-02-19 15:12
 */
@MvcAction	
@ReqMapping("/admin/crm/trackstatus")
public class TrackStatusController{
	
	@Inject
	private JdbcSession sqlSession;
	
    @HasPermission("crm:trackstatus:view")
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
    	final Page<TrackStatus> page = sqlSession.findPage(TrackStatus.class)
    			.eq(jsonObject, "companyNo", "id")
    			.like(jsonObject, "name")
				.eq("deleted", false)
				.page(jsonObject)
				.order("sort", "desc")
				.exe();
    	
    	return Result.success(page);
    }

	@HasPermission("crm:trackstatus:view")
	@ReqMapping("/listAll")
	public Object listAll(JSONObject jsonObject) {
		return sqlSession.findArray(TrackStatus.class)
				.eq(jsonObject, "companyNo", "id")
				.like(jsonObject, "name")
				.eq("deleted", false)
				.order("sort", "desc")
				.exe();
	}

    @HasPermission("crm:trackstatus:view")
    @ReqMapping("/get")
    public Object get(String id) {
    	TrackStatus word = Pool.get(TrackStatus.class, id);
    	return Result.success(word);
    }


	@Bean(newInstance=true, copy=true)
    @HasPermission("crm:trackstatus:edit")
    @ReqMapping("/save")
    public Object save(TrackStatus item, ValidResult result) {
    	if (result.hasError()) {
    		return Result.error(0, result.getErrorMessage());
    	}

    	sqlSession.insert(item);
    	return Result.success();
    }


	@Bean(copy=true)
    @HasPermission("crm:trackstatus:edit")
    @ReqMapping("/update")
    public Object update(TrackStatus item, ValidResult result) {
    	if (result.hasError()) {
    		return Result.error(0, result.getErrorMessage());
    	}

    	sqlSession.updateById(item);
    	return Result.success();
    }

    @HasPermission("crm:trackstatus:edit")
    @ReqMapping("/delete")
    public Object delete(String id) {
    	TrackStatus item = Pool.get(TrackStatus.class, id);
    	
    	if (item != null) {
    		item.setDeleted(true);
    		sqlSession.updateById(item);
    	}
    	return Result.success();
    }
    
    @HasPermission("crm:trackstatus:edit")
    @ReqMapping("/order")
    public Object order(String id, Integer sort) {
    	TrackStatus item = Pool.get(TrackStatus.class, id);
    	
    	if (item != null) {
    		item.setSort(sort);
    		sqlSession.updateById(item);
    	}
    	return Result.success();
    }

}
